package cn.szzsi._web;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.core.kit.HttpRequestUtils;
import cn.szzsi.common.model.UserIp;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

import java.util.Date;

/**
 * 获取用户IP地址
 * Created by admin on 2017/5/27.
 */
public class IpController extends Controller {
    public void getIp(){
        getResponse().setHeader("Access-Control-Allow-Origin","*");
        getResponse().setHeader("Access-Control-Allow-Methods","GET,POST");
    }

    public void index(){
       //new Thread()
       getResponse().setHeader("Access-Control-Allow-Origin","*");
       getResponse().setHeader("Access-Control-Allow-Methods","GET,POST");
       String shopId=getPara("shopId");
       String ip= null;
       try {
           ip = HttpRequestUtils.getIpAddr(getRequest());
       } catch (Exception e) {
           e.printStackTrace();
       }

       UserIp userIp=new UserIp();
       userIp.set("id", StrKit.getRandomUUID());
       userIp.set("ip",ip);
       userIp.set("createDate",new Date());
       if(CommonUtil.isMobileDevice(getRequest())){//手机访问还是电脑访问
           userIp.set("deviceType","0");
       }else{
           userIp.set("deviceType","1");
       }
       String agent=getRequest().getHeader("User-Agent").toLowerCase();
       userIp.set("browserType",getBrowserType(agent));
       userIp.set("shopId",shopId);
       userIp.save();

       renderNull();
    }
    /**
     * 获取浏览器版本信息
     * @Title: getBrowserName
     * @data:2015-1-12下午05:08:49
     * @author:wolf
     *
     * @param agent
     * @return
     */
    public String getBrowserType(String agent) {
        if(agent.indexOf("msie")>0 ||(agent.indexOf("gecko")>0 && agent.indexOf("rv:")>0) ){//IE浏览器1
            return "1";
        }else if(agent.indexOf("firefox")>0){//火狐浏览器2
            return "2";
        }else if(agent.indexOf("ucbrowser")>0 || agent.indexOf("ubrowser")>0){//UC浏览器3
            return "3";
        }else if(agent.indexOf("qqbrowser")>0){//QQ浏览器4
            return "4";
        }else if(agent.indexOf("theworld")>0){//世界之窗浏览器5
            return "5";
        }else if(agent.indexOf("chrome")>0){//谷歌浏览器6
            return "6";
        }else{
            return "0";
        }
    }



}
